package com.cskaoyan.gateway.controller.user;


import com.cskaoyan.gateway.bean.bo.LoginBo;
import com.cskaoyan.gateway.bean.bo.RegisterBo;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IMemberService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class MemberController {

    @Reference(timeout = 3000,check = false)
    IMemberService memberService;

    @Autowired
    CaptchaController captchaController;



    @PostMapping("/login")
    @Anoymous
    public ResponseData userLogin(@RequestBody LoginBo loginBo, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
    {
        //验证码和uuid封装进验证码request,uuid从cookie里取，这里为了省事儿直接复用captchaController了
        // 验证，通过往下走，不通过返回
        ResponseData responseData = captchaController.validKaptchaCode(loginBo.getCaptcha(), httpServletRequest);
        if (responseData.getCode() != 200)
        {
            return responseData;
        }
        //账户密码封装进入用户Request
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setPassword(loginBo.getUserPwd());
        userLoginRequest.setUserName(loginBo.getUserName());
        UserLoginResponse userLoginResponse = memberService.userLogin(userLoginRequest);

        if(userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode()))
        {
            Cookie cookie= CookieUtil.genCookie("access_token",userLoginResponse.getToken(),"/",6000000);
            cookie.setHttpOnly(true);
            httpServletResponse.addCookie(cookie);
            return new ResponseUtil<>().setData(userLoginResponse);
        }
        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }

    @GetMapping("/login")
    public ResponseData loginGet(HttpServletRequest httpServletRequest)
    {
        UserSimpleInfo userSimpleInfo = memberService.userLoginGet(CookieUtil.getCookieValue(httpServletRequest, "access_token"));

        return new ResponseUtil<>().setData(userSimpleInfo);

    }

    @Anoymous
    @PostMapping("/register")
    public ResponseData userRegister(@RequestBody RegisterBo registerBo,HttpServletRequest httpServletRequest)
    {
        //验证码和uuid封装进验证码request,uuid从cookie里取，这里为了省事儿直接复用captchaController了
        // 验证，通过往下走，不通过返回
        ResponseData responseData = captchaController.validKaptchaCode(registerBo.getCaptcha(), httpServletRequest);
        if (responseData.getCode() != 200)
        {
            return responseData;
        }

        //账户密码邮箱封装进入Request
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(registerBo.getEmail());
        userRegisterRequest.setUserName(registerBo.getUserName());
        userRegisterRequest.setUserPwd(registerBo.getUserPwd());

        UserRegisterResponse userRegisterResponse = memberService.userRegister(userRegisterRequest);

        if(userRegisterResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode()))
        {
            return new ResponseUtil<>().setData(null);
        }

        return new ResponseUtil<>().setErrorMsg(userRegisterResponse.getMsg());
    }

    @GetMapping("/verify")
    @Anoymous
    public ResponseData userVerify(String uid,String username)
    {
        //封装Request
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(username);
        userVerifyRequest.setUuid(uid);
        UserVerifyResponse userVerifyResponse = memberService.userVerify(userVerifyRequest);

        if(userVerifyResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode()))
        {
            return new ResponseUtil<>().setData(null);
        }

        return new ResponseUtil<>().setErrorMsg(userVerifyResponse.getMsg());
    }

    @GetMapping("/loginOut")
    public ResponseData userLogout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)
    {
        Cookie cookie =null;
        Cookie[] cookies = httpServletRequest.getCookies();
        for (Cookie cookie1 : cookies) {
            if("access_token".equals(cookie1.getName()))
            {
                cookie = cookie1;
            }
        }
        cookie.setMaxAge(0);
        cookie.setPath("/");

        httpServletResponse.addCookie(cookie);

        return new ResponseUtil<>().setData(null);
    }


}
